//
//  OosEnum.m
//  oos4ios
//
//  Created by Pablo Gonzalez on 3/4/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <oos4ios/OosEnum.h>
#import "OosEnum+Internal.h"
#import <oos4ios/Exceptions/MethodNotImplementedException.h>

/**
 The extension of 
 */
@implementation OosEnum

@synthesize description = mDescription;
@synthesize ordinal = mOrdinal;

/**
 
 */
+ (NSArray *) values
{
	[MethodNotImplementedException raiseForMethod:@"values:" inObject:self];
	return nil;
}

+ (NSMutableDictionary *) enums
{
	[MethodNotImplementedException raiseForMethod:@"enums:" inObject:self];
	return nil;
}

+ (id) enumWithDescription:(NSString *)description 
					   andOrdinal:(NSUInteger)ordinal
{
	return [[[[self class] alloc] initWithDescription:description 
									  andOrdinal:ordinal] autorelease];
}

- (id) initWithDescription:(NSString *)description 
					   andOrdinal:(NSUInteger)ordinal
{
	self = [super init];
	if (self) {
		mDescription = [description copy];
		mOrdinal = ordinal;
	}
	return self;
}

+ (id) enumForPosition: (NSUInteger) position 
            andLiteral: (NSString *) literal
{
    NSDictionary *enums = [self enums];
    id result = [enums objectForKey:literal];
    if (!result) {
		result = [[self class] enumWithDescription:literal 
										andOrdinal:position];
		[[self enums] setValue:result forKey:literal];
	}
	return result;
}

+ (id) enumForPosition: (NSUInteger) position
{
    NSArray *values = [self values];
    if (position < values.count) {
        NSString *literal = [[self values] objectAtIndex:position];
        return [self enumForPosition:position 
                          andLiteral:literal];
    } else {
        NSLog(@"enumForPosition: %d in %@", position, values);
        return nil;
    }
}

+ (id) enumFromLiteral: (NSString *) literal
{
    NSUInteger position = [[self values] indexOfObject:literal];
    if (position != NSNotFound) {
        return [self enumForPosition:position 
                          andLiteral:literal];
    }
	return nil;
}

- (void)dealloc
{
    [mDescription release];
    [super dealloc];
}

@end
